package com.cib.xagcjs.web;

import com.alibaba.fastjson.JSONObject;
import com.cib.xagcjs.common.Result;
import com.cib.xagcjs.common.RmbRequest;
import com.cib.xagcjs.common.RmbResponse;
import com.cib.xagcjs.utils.CryptoUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 测试入口
 */
@Controller
@RequestMapping(value = "/test")
@Slf4j
public class PayWeb {

    @Autowired
    private CryptoUtils cryptoUtils;

    /**
     * 解密
     * @param req 加密的数据
     * @return 解密后的数据
     */
    @PostMapping("/demo")
    @ResponseBody
    public RmbResponse jie(@RequestBody String req) {
        RmbResponse rmbResponse = new RmbResponse();
        Result<Object> success = Result.success(cryptoUtils.decryptAndVerify(req),"解密成功！！！！");
        rmbResponse.setResult(success);
        return rmbResponse;
    }

    /**
     * 加密
     * @param req 需要加密的数据
     * @return 加密后的数据
     */
    @PostMapping("/demo2")
    @ResponseBody
    public RmbResponse jia(@RequestBody String req) throws Exception {
        RmbResponse rmbResponse = new RmbResponse();
        Result<Object> success = Result.success(cryptoUtils.encryptAndSign(req),"加密成功！！！！");
        rmbResponse.setResult(success);
        return rmbResponse;
    }

    /**
     * 测试
     * @param req 需要加密的数据
     * @return 加密后的数据
     */
    @PostMapping("/demo3")
    @ResponseBody
    public RmbResponse test(@RequestBody String req) {
        RmbResponse rmbResponse = new RmbResponse();
        RmbRequest rmbRequest = JSONObject.parseObject(req, RmbRequest.class);
        log.info("asfdsad" + JSONObject.toJSONString(rmbRequest));

        Result<Object> success = Result.success();
        rmbResponse.setResult(success);
        return rmbResponse;
    }
}
